package com.zzg.dao;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import com.zzg.entity.inform;
import com.zzg.util.JDBCUtils;

public  class InformDao {
	
	public List<Map<String, Object>> findAll(int index,int pageSize,String key,String aname,String role) {
		String sql="";
//		System.out.println(role);
		if(role.equals("student") | role.equals("teacher")) {
			sql="select * from inform where ititle like ? and role=? order by iDate desc limit ?,?";
			Object params[]={
					key,role,index,pageSize
			};
			return JDBCUtils.query(sql,params);
		}else {
			sql="select * from inform where ititle like ? and aname=? order by iDate desc limit ?,?";
			Object params[]={
					key,aname,index,pageSize
			};
			return JDBCUtils.query(sql,params);
		}
//		return JDBCUtils.query(sql,param);
	}
	public List<Map<String, Object>> findCount(String key,String aname,String role) {
		String sql="";
		System.out.println(key);
		System.out.println(role);
		if(role.equals("student") | role.equals("teacher")) {
			sql="select count(id) from inform where ititle like ? and role=?";
			Object params[]={
					key,role
			};
			return JDBCUtils.query(sql,params);
		}else {
			sql="select count(id) from inform where ititle like ? and aname=?";
			Object params[]={
					key,aname
			};
			return JDBCUtils.query(sql,params);
		}
		
	}
	
	public void insert(inform inform) {
		String sql="insert into inform(role,ititle,icontent,aname,iDate) values(?,?,?,?,?)";
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
		String date = dateformat.format(System.currentTimeMillis());
		System.out.println(date);
		Object params[]={
				inform.getRole(),inform.getItitle(),inform.getIcontent(),inform.getAname(),date
		};
		JDBCUtils.update(sql, params);
	}
	public void edit(inform inform) {
		String sql="update forum set role=?,ititle=?,icontent=?,aname=?,iDate=? where id=?";
		Object params[]={
				inform.getRole(),inform.getItitle(),inform.getIcontent(),inform.getAname(),new java.sql.Date(inform.getiDate().getTime()),inform.getId()
		};
		JDBCUtils.update(sql, params);
	}
	public List<Map<String, Object>> findById(Integer id) {
		// TODO Auto-generated method stub
		String sql="select * from inform where id=?";
		return JDBCUtils.query(sql,id);
	}
	
	public List<Map<String, Object>> findById(String id) {
		String sql="select * from inform where id=?";
		return JDBCUtils.query(sql,Integer.parseInt(id));
	}
	
	public void delById(String id) {
		String sql="delete from inform where id=?";
		JDBCUtils.update(sql, Integer.parseInt(id));
	}
	public void delete(String[] ids){
		/*
		 * 1.拼出删除的sql语句DELETE FROM tb_news WHERE id IN(?,?,?)
		 * 2.生成对应的参数数组
		 * 3.调用JDBCUtils删除
		 */
		StringBuilder sql = new StringBuilder("DELETE FROM inform WHERE id IN(");
		Integer params[] = new Integer[ids.length];
		for(int i=0;i<ids.length;i++){
			/*
			 * 1.判断是否是最后一个
			 * 2.给参数？设置对应的值
			 */
			if(i==ids.length-1){
				//是最后一个
				sql.append("?)");
			}else{
				sql.append("?,");
			}
			params[i] = new Integer(ids[i]);
		}
		JDBCUtils.update(sql.toString(), params);
	}
	
	
}
